package admin

import (
	"chaowan/data_source"
	"chaowan/models"
	"github.com/gin-gonic/gin"
	"net/http"
)

func Packages(ctx *gin.Context) {

	var goodtype []models.Package
	data_source.Db.Find(&goodtype)

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    goodtype,
		"msg":     "ok",
	})
	return
}

type PackageBody struct {
	ID    int64   `json:"id" gorm:"id"`
	Price float64 `json:"price" gorm:"price"`
	Daibi float64 `json:"daibi" gorm:"daibi"`
	Num   int64   `json:"num" gorm:"num"`   // 限购数量  0不限
	Sort  int64   `json:"sort" gorm:"sort"` // 排序 数字越大排序越靠前
}

func Packageadd(ctx *gin.Context) {
	request := PackageBody{}
	ctx.ShouldBind(&request)
	price := request.Price
	daibi := request.Daibi
	num := request.Num
	sort := request.Sort
	gt := models.Package{
		Price: price,
		Daibi: daibi,
		Num:   num,
		Sort:  sort,
	}
	data_source.Db.Create(&gt)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}
func Packageupdate(ctx *gin.Context) {
	request := PackageBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	price := request.Price
	daibi := request.Daibi
	num := request.Num
	sort := request.Sort
	gt := models.Package{
		Price: price,
		Daibi: daibi,
		Num:   num,
		Sort:  sort,
	}
	data_source.Db.Where("id = ?", id).Updates(&gt)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}

func Packagedel(ctx *gin.Context) {
	request := PackageBody{}
	ctx.ShouldBind(&request)
	id := request.ID

	data_source.Db.Where("id = ?", id).Delete(&models.Package{})
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}
